import { IPC_CHANNELS } from 'shared/ipcChannels'

/**
 * 前端日志工具，将日志发送到主进程显示在运行日志中
 */
export class FrontendLogger {
  private scope: string

  constructor(scope: string) {
    this.scope = scope
  }

  private sendLog(level: 'info' | 'warn' | 'error' | 'success', message: string) {
    window.ipcRenderer.invoke(IPC_CHANNELS.sendLog, {
      scope: this.scope,
      level,
      message,
    })
  }

  info(message: string) {
    this.sendLog('info', message)
  }

  warn(message: string) {
    this.sendLog('warn', message)
  }

  error(message: string) {
    this.sendLog('error', message)
  }

  success(message: string) {
    this.sendLog('success', message)
  }
}

/**
 * 创建一个前端logger实例
 */
export function createFrontendLogger(scope: string): FrontendLogger {
  return new FrontendLogger(scope)
}

